package com.fortune.controller.app;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.AccountDAO;
import com.fortune.dal.dao.BankCardDAO;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.TradeQueryDAO;
import com.fortune.dal.model.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cash")
public class CashAccountController {
    @Autowired
    private HttpSession httpSession;

    @Autowired
    private AccountDAO accountDAO;

    @Autowired
    private BankCardDAO bankCardDAO;

    @Autowired
    private BankDAO bankDAO;

    @Autowired
    private TradeQueryDAO tradeQueryDAO;

    @RequestMapping(value = "cashfir", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws BusinessException {
        ModelAndView mv = new ModelAndView("cashfir");
        Cust u = (Cust)httpSession.getAttribute("user");
        Account param = new Account();
        param.setCustId(u.getId());
        try {
            List<Account> list = accountDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("该用户未注册一卡通账户");
            }

            Account account = list.get(0);
            Double allBalance = account.getAccountBalance();
            httpSession.setAttribute("allBalance",allBalance);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return mv;
    }

    @RequestMapping(value = "cashsec", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws BusinessException {
        ModelAndView mv = new ModelAndView("cashsec");
        mv.addObject("menuFlag", "cashsec");
        return mv;
    }


    //选择银行卡
    @RequestMapping(value = "cashchosecard", method = RequestMethod.GET)
    public ModelAndView cashInCard() throws BusinessException {
        ModelAndView mv = new ModelAndView("cashchosecard");

        Cust cust = (Cust)httpSession.getAttribute("user");
        BankCard bankCard = new BankCard();
        bankCard.setCustId(cust.getId());
        List<Map<String,Object>> lists = new ArrayList();
        try {
            List<BankCard> list = bankCardDAO.queryByModel(bankCard);
            for(BankCard bc : list){
                Map<String,Object> map = MapUtils.parseMap(bc);
                Bank bank = bankDAO.queryById(bc.getBankId());
                String bankName = bank.getName();
                map.put("bankName",bankName);
                String imgUrl = bank.getImgUrl();
                map.put("imgUrl",imgUrl);
                String cardCode = bc.getCardCode();
                String simpleCode = cardCode.substring(cardCode.length() - 4);
                map.put("simpleCode",simpleCode);
                lists.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mv.addObject("lists", lists);
        return mv;
    }

    //选卡成功
    @RequestMapping(value = "afterchosecard", method = RequestMethod.GET)
    public ModelAndView afterChoseCard(@RequestParam Integer id) throws BusinessException {
        ModelAndView mv = new ModelAndView("cashfir");
        try {
            BankCard b = new BankCard();
            b.setId(id);
            BankCard bankCard = bankCardDAO.queryById(b.getId());
            httpSession.setAttribute("selectCard",bankCard);
            Map<String,Object> map = MapUtils.parseMap(bankCard);
            String cardCode = bankCard.getCardCode();
            String newCode = cardCode.substring(cardCode.length() - 4);
            map.put("newCode",newCode);
            Bank bank = bankDAO.queryById(bankCard.getBankId());
            String bankName = bank.getName();
            map.put("newName",bankName);

            httpSession.setAttribute("cardMap",map);

            mv.addObject("cardMap", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }


    @RequestMapping(value = "cashmoney", method = RequestMethod.POST)
    public BaseResp cashMoney(@RequestBody TradeQuery tradeQuery) throws BusinessException {
        BaseResp resp = new BaseResp();
        try{
            Double money = tradeQuery.getTradeAmount();
            if(StringUtils.isBlank(money+"")){
                throw new BusinessException("输入金额不能为空");
            }
            httpSession.setAttribute("oldCashMoney",money);
            httpSession.setAttribute("newCashMoney",money - 10);

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    //验证交易密码  将交易记录存入数据库
    @RequestMapping(value = "cashpass", method = RequestMethod.POST)
    public BaseResp cashPass(@RequestBody Cust cust) throws BusinessException {
        BaseResp resp = new BaseResp();
        try{
            if(StringUtils.isBlank(cust.getTradePassword())){
                throw new BusinessException("交易密码不能为空");
            }
            Cust c = (Cust) httpSession.getAttribute("user");
            if(!(c.getTradePassword().equals(cust.getTradePassword()))){
                throw new BusinessException("交易密码错误");
            }

            Account account = new Account();
            account.setCustId(c.getId());
            List<Account> accountList = accountDAO.queryByModel(account);
            if(ListUtils.isEmpty(accountList)){
                throw new BusinessException("未注册账户");
            }
            Account accounts = accountList.get(0);
            Double oldAccountBalance = accounts.getAccountBalance();
            Double cashMoney = (Double)httpSession.getAttribute("newCashMoney");
            Double newAccountBalance = oldAccountBalance - cashMoney;

            accounts.setAccountBalance(newAccountBalance);
            accountDAO.updateById(accounts);

            //将交易记录存入数据库
            TradeQuery tradeQuery = new TradeQuery();
            tradeQuery.setTradeOperator("取现");
            tradeQuery.setTradeDate(new Date());
            Double money = (Double)httpSession.getAttribute("oldCashMoney");
            tradeQuery.setTradeAmount(money);
            tradeQuery.setTradeStatus("交易成功");
            tradeQuery.setCustId(c.getId());
            BankCard bankCard = (BankCard) httpSession.getAttribute("selectCard");
            tradeQuery.setBankCardId(bankCard.getId());

            tradeQueryDAO.insert(tradeQuery);

            //删除session

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "cashsuccess", method = RequestMethod.GET)
    public ModelAndView indexOfAllThree() throws BusinessException {
        ModelAndView mv = new ModelAndView("cashsuccess");

//        httpSession.removeAttribute("allBalance");
//        httpSession.removeAttribute("cardMap");

        mv.addObject("menuFlag", "cashsuccess");
        return mv;
    }
}
